#include "headfiles.h"

using namespace std;

bool findDupNums(const char *file_name) {
    if (file_name == NULL) {
        cout << "Invalid file name!" << endl;
        return false;
    }

    size_t max_val = 32000;
    size_t bit_vec_size = max_val / 8;
    unsigned char *bit_vec = (unsigned char *)malloc(sizeof(unsigned char) * bit_vec_size);
    if (bit_vec == NULL) {
        cout << "Allocate bit vector buffer error!" << endl;
        return false;
    }

    memset(bit_vec, 0, sizeof(unsigned char) * bit_vec_size);

    fstream fs;
    string str;
    fs.open(file_name, fstream::in);
    if (!fs.is_open()) {
        cout << "File " << file_name << " open error!" << endl;
        return false;
    }

    while (fs >> str) {
        if (str.empty()) {
            break;
        }

        unsigned long tmp_val = strtoul(str.c_str(), NULL, 10);
        unsigned long byte_index = tmp_val >> 3;
        unsigned long bit_index = tmp_val & 7;
        unsigned char byte_val = 1 << bit_index;
        if (byte_val & bit_vec[byte_index]) {
            cout << tmp_val << endl;
        }
        bit_vec[byte_index] |= byte_val;
    }

    return true;
}

